﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace _0812045
{
    public class Tank : VisibleGameEntity
    {
        private BulletManager bulletManager = new BulletManager();
        int delayToFire = 0;
        public override bool Init(ContentManager content, int n, string strResource)
        {
            // Khoi tao nhan vat
            _nsprite = 3;
            _sprite = new BaseSprite[_nsprite];
            Texture2D[] texture2D;
            texture2D = new Texture2D[n];
            for (int i = 0; i < n; i++)
                texture2D[i] = content.Load<Texture2D>(@"Tank/" + strResource);
            Texture2D track = content.Load<Texture2D>(@"Tank/tracks");
            _sprite[0] = new TankSprite(texture2D, track);

            Texture2D[] texture2Dshadow = new Texture2D[1];
            texture2Dshadow[0] = content.Load<Texture2D>(@"Tank/tankshadow");
            _sprite[1] = new PictureSprite(texture2Dshadow);

            Texture2D[] texture2Dgun = new Texture2D[1];
            texture2Dgun[0] = content.Load<Texture2D>(@"Tank/gun");
            _sprite[2] = new OnePicSprite(texture2Dgun);

            bulletManager.InitPrototypes(content);

            return true;
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            Vector2 pointToDrawShadow = new Vector2();
            pointToDrawShadow.X = _sprite[0].x;
            pointToDrawShadow.Y = _sprite[0].y + 35;
            ((PictureSprite)_sprite[1]).Draw(gameTime, spriteBatch, pointToDrawShadow);


            _sprite[2].x = _sprite[0].x;
            _sprite[2].y = _sprite[0].y - 25;
            _sprite[2].Draw(gameTime, spriteBatch);

            _sprite[0].Draw(gameTime, spriteBatch);
            for (int i = 0; i < bullets.Count; i++)
                bullets[i].Draw(gameTime,spriteBatch);
        }

        public override void Update(GameTime gameTime)
        {
            MouseState ms = Mouse.GetState();
            
            
            int Mx = ms.X;
            int My = ms.Y;
            if (ms.X < 28)
                Mx = 28;
            if (ms.Y < 28)
                My = 28;
            if (ms.X > 612)
                Mx = 612;
            if (ms.Y > 425)
                My = 425;
            float deltaX = -_sprite[2].x - 40 + Mx;
            float deltaY = -_sprite[2].y - 25 + My;
            double degree = Math.Acos((deltaX) / (Math.Sqrt(deltaX * deltaX + deltaY * deltaY)));
            double goc = degree * 180 / Math.PI;

            _sprite[2].itexture2d = 20 - (int)(goc / 9);

            if (delayToFire == 0)
                if (ms.LeftButton == ButtonState.Pressed)
                    Fire(bulletManager, _sprite[2].itexture2d);
            
            if (ms.LeftButton == ButtonState.Released)
                delayToFire = 0;
            else
                delayToFire = (delayToFire + 1) % 10;

            for (int i = 0; i < bullets.Count; i++)
            {
                if (((BulletSprite)bullets[i]._sprite[0]).visible == false)
                    bullets.RemoveAt(i);
            }

            for (int i = 0; i < bullets.Count; i++)
                bullets[i].Update(gameTime);
            base.Update(gameTime);
        }

        List<Bullet> bullets = new List<Bullet>();

        public void Fire(BulletManager bulletManager, int iTexture)
        {

            Bullet bullet = (Bullet)bulletManager.CreateObject(0);
            //bullet.nDelay = 3;
            bullet._sprite[0].itexture2d = iTexture;
            bullet.x = _sprite[2].x + 28 + 39 * (float)Math.Sin((_sprite[2].itexture2d - 10) * 9 * Math.PI / 180);
            //y -= 5 * (float)Math.Cos(Math.Abs(_itexture2d - 10) * 9 * Math.PI / 180);
            //x += 5 * (float)Math.Sin((_itexture2d - 10) * 9 * Math.PI / 180);
            bullet.y = _sprite[2].y - 10 + 39 - 39 * (float)Math.Cos(Math.Abs(_sprite[2].itexture2d - 10) * 9 * Math.PI / 180);
            bullets.Add(bullet);
        }
    }
}
